<?php

/* function count_tf_0() { //mengembalikan array raw tf term di setiap dokumen ---- ga dipake
  $i = "";

  $title_string = ".T";
  $author_string = ".A";
  $body_string = ".W";
  $index_string = ".I";

  $is_index = false;
  $is_title = false;
  $is_author = false;
  $is_body = false;

  $index_term = array();

  if ($is_document_collection_custom) { //pake uploads/customdocumentcollection.txt - $file_collection_custom
  $file_col = fopen($file_collection_custom, "r");

  //print_r($index_term);
  } else { // //pake uploads/defaultdocumentcollection.txt
  $file_collection_custom = "uploads/defaultdocumentcollection.txt";
  $file_col = fopen($file_collection_custom, "r");
  }

  if ($file_col) {
  while (!feof($file_col)) {
  if (($temp_string = fgetcsv($file_col, 1000, " ")) !== FALSE) {

  if (strncasecmp($temp_string[0], $index_string, 2) == 0) {
  //proses pembuatan array of term di dokumen
  $i = $temp_string[1];
  $is_index = true;
  } else if (strncasecmp($temp_string[0], $title_string, 2) == 0) {
  //proses indexing di title
  $is_title = true;
  } else if (strncasecmp($temp_string[0], $author_string, 2) == 0) {
  //proses indexing di author
  $is_author = true;
  } else if (strncasecmp($temp_string[0], $body_string, 2) == 0) {
  //proses indexing di body
  $is_body = true;
  } else if ((strncasecmp(substr($temp_string[0], 0, 1), ".", 1) == 0) &&
  ((strncasecmp(substr($temp_string[0], 1, 1), "I", 1) != 0) ||
  (strncasecmp(substr($temp_string[0], 1, 1), "T", 1) != 0) ||
  (strncasecmp(substr($temp_string[0], 1, 1), "A", 1) != 0) ||
  (strncasecmp(substr($temp_string[0], 1, 1), "B", 1) != 0))) {

  $is_index = false;
  $is_title = false;
  $is_author = false;
  $is_body = false;
  }


  if (($is_title || $is_author || $is_body)) {

  if ((strncasecmp($temp_string[0], $index_string, 2) != 0) &&
  (strncasecmp($temp_string[0], $title_string, 2) != 0) &&
  (strncasecmp($temp_string[0], $author_string, 2) != 0) &&
  (strncasecmp($temp_string[0], $body_string, 2) != 0)) {

  foreach ($temp_string as $temp) {
  $temp2 = strtolower($temp);
  if (!strrpos($temp2, " ")) {
  if (strncasecmp($temp2, "", 1) != 0) {
  if (isset($index_term[$i][$temp2])) {
  ++$index_term[$i][$temp2];
  } else {
  $index_term[$i][$temp2] = 1;
  }
  }
  }
  }
  }
  }
  }
  }
  }

  return $index_term;
  } */

function is_query_contained($query_array, $term) {
    $result = false;
    foreach ($query_array as $query_el) {
        if (strcasecmp($query_el, $term) == 0) {
            $result = true;
            return $result;
        }
    }
    return $result;
}

function set_counter($counter){
    $file_counter = "counter.txt";
    $file_count = fopen($file_counter, "w");
    if ($file_count){
        fwrite($file_count, $counter);
    }
    fclose($file_count);
}

function get_counter(){
    $file_counter = "counter.txt";
    $file_con = fopen($file_counter, "r");
    if ($file_con){
        $counter = fgets($file_con);
    }
    fclose($file_con);
    return $counter;
}

function count_tf_1($query_array, $is_stemming, $is_eliminate_stopword, $stopwords, $file_collection) {
    $i = "";
    $counter = 0;

    $title_string = ".T";
    $author_string = ".A";
    $body_string = ".W";
    $index_string = ".I";
    $publisher_string = ".B";

    $is_index = false;
    $is_title = false;
    $is_author = false;
    $is_body = false;
    $is_publisher = false;
    
    $stemmer = new Stemmer();

    $index_term = array();
    $file_col = fopen($file_collection, "r");

    if ($file_col) {
        while (!feof($file_col)) {
            if (($temp_string = fgetcsv($file_col, 10000, " ")) !== FALSE) {

                if (strncasecmp($temp_string[0], $index_string, 2) == 0) {
                    //proses pembuatan array of term di dokumen
                    $i = $temp_string[1];
                    ++$counter;
                    $is_index = true;
                } else if (strncasecmp($temp_string[0], $title_string, 2) == 0) {
                    //proses indexing di title
                    $is_title = true;
                } else if (strncasecmp($temp_string[0], $publisher_string, 2) == 0) {
                    //proses indexing di publisher
                    $is_publisher = true;
                }else if (strncasecmp($temp_string[0], $author_string, 2) == 0) {
                    //proses indexing di author
                    $is_author = true;
                } else if (strncasecmp($temp_string[0], $body_string, 2) == 0) {
                    //proses indexing di body
                    $is_body = true;
                } else if ((strncasecmp(substr($temp_string[0], 0, 1), ".", 1) == 0) &&
                        ((strncasecmp(substr($temp_string[0], 1, 1), "I", 1) != 0) ||
                        (strncasecmp(substr($temp_string[0], 1, 1), "T", 1) != 0) ||
                        (strncasecmp(substr($temp_string[0], 1, 1), "A", 1) != 0) ||
                        (strncasecmp(substr($temp_string[0], 1, 1), "B", 1) != 0) ||
                        (strncasecmp(substr($temp_string[0], 1, 1), "W", 1) != 0))) {

                    $is_index = false;
                    $is_title = false;
                    $is_author = false;
                    $is_body = false;
                }


                if (($is_title || $is_author || $is_body || $is_publisher)) {

                    if ((strncasecmp($temp_string[0], $index_string, 2) != 0) &&
                            (strncasecmp($temp_string[0], $title_string, 2) != 0) &&
                            (strncasecmp($temp_string[0], $author_string, 2) != 0) &&
                            (strncasecmp($temp_string[0], $body_string, 2) != 0) &&
                            (strncasecmp($temp_string[0], $publisher_string, 2) != 0)) {

                        foreach ($temp_string as $temp) {
                            $temp2 = strtolower($temp);
                            if (!$is_eliminate_stopword || !is_stopword($stopwords, $temp2)) { // disini buang stopwordnya
                                if (!strrpos($temp2, " ")) {
                                    if ($is_stemming) {
                                        $temp2 = $stemmer->stem($temp2); // di sini stemmingnya
                                    }
                                    if (is_query_contained($query_array, $temp2)) {
                                        if (strcasecmp($temp2, "") != 0) {
                                            if (isset($index_term[$i][$temp2])) {
                                                ++$index_term[$i][$temp2];
                                            } else {
                                                $index_term[$i][$temp2] = 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    fclose($file_col);
    set_counter($counter);
    return $index_term;
}

function raw_tf($tf) {
    return $tf;
}

function binary_tf($tf) {
    $i = "";
    $j = "";
    $a = $v = 0;
    $log_array = array();
    //$temp_array = count_tf();
    $temp_array = $tf;
    foreach ($temp_array as $i => $temp) {
        foreach ($temp as $j => $v) {
            $log_array[$i][$j] = 1;
        }
    }
    return $log_array;
    //print_r($log_array);
}

function log_tf($tf) {
    $i = "";
    $j = "";
    $a = $v = 0;
    $log_array = array();
    //$temp_array = count_tf();
    $temp_array = $tf;
    foreach ($temp_array as $i => $temp) {
        foreach ($temp as $j => $v) {
            if ($v != 0) {
                $a = 1 + log10($v);
                $log_array[$i][$j] = $a;
            } else if ($v == 0) {
                $log_array[$i][$j] = 0;
            }
        }
    }
    return $log_array;
    //print_r($log_array);
}

function augmented_tf($tf) {
    $i = "";
    $j = "";
    $a = $v = 0;
    $temp_max = 0;
    $log_array = array();
    //$temp_array = count_tf();
    $temp_array = $tf;
    foreach ($temp_array as $i => $temp) {
        $temp_max = max($temp);
        //echo "ini max buat ".$i." : ".$temp_max."</br>";
        foreach ($temp as $j => $v) {
            $a = 0.5 + 0.5 * $v / $temp_max;
            $log_array[$i][$j] = $a;
        }
    }
    return $log_array;
    //print_r($log_array);
}

function is_contained($tf, $index, $term) {
    $result = false;
    if (isset($tf[$index][$term])) {
        $result = true;
    }
    return $result;
}

function get_ndoc_contained($tf, $term) {
    $i = $j = "";
    $n = 0;
    $is_con = false;
    foreach ($tf as $i => $temp) {
        $is_con = is_contained($tf, $i, $term);
        if ($is_con) {
            ++$n;
        }
    }
    return $n;
}


function idf_builder($tf) {
    $i = "";
    $j = "";
    $a = $b = $v = 0;
    $temp_n = 0;
    $log_array = array();
    //$temp_array = count_tf();
    $temp_array = $tf;
    //$N_doc = count($temp_array);
    $N_doc = get_counter();
    foreach ($temp_array as $i => $temp) {
        foreach ($temp as $j => $v) {
            if (!isset($log_array[$j])) {
                $temp_n = get_ndoc_contained($tf, $j);
                if ($temp_n != 0) {
                    $a = $N_doc / $temp_n;
                    $b = log10($a);
                } else {
                    $b = 0;
                }

                $log_array[$j] = $b;
            }
        }
    }
    return $log_array;
    //print_r($log_array);
}

function normalization_doc_builder($tf, $tf_method) {
    // raw, logarithmic, binary, augmented
    $i = $j = "";
    $nor_array = array();
    $temp_array = array();
    $temp_value = 0;
    $a = 0;
    if (strcasecmp($tf_method, "raw") == 0) {
        $temp_array = raw_tf($tf);
    } else if (strcasecmp($tf_method, "logarithmic") == 0) {
        $temp_array = log_tf($tf);
    } else if (strcasecmp($tf_method, "binary") == 0) {
        $temp_array = binary_tf($tf);
    } else if (strcasecmp($tf_method, "augmented") == 0) {
        $temp_array = augmented_tf($tf);
    }
    
    
    foreach ($temp_array as $i => $temp_1){
        $temp_value = 0;
        foreach ($temp_1 as $j => $v){
            $a = $v * $v;
            $temp_value += $a;
        }
        $nor_array[$i] = sqrt($temp_value);
    }
    
    return $nor_array;
}

function term_weighting_doc($tf, $is_idf, $is_normalization, $tf_method){
    $array_result = array();
    $i = $j = "";
    $a = $b = 0;
    $array_idf = array();
    $array_norm = array();
    $array_temp = array();
    if (!$is_idf && !$is_normalization){
        $array_result = $tf;
    }
    else if ($is_idf && !$is_normalization){
        $array_idf = idf_builder($tf);
        foreach ($tf as $i => $temp1){
            foreach ($temp1 as $j => $v){
                $a = $v * $array_idf[$j];
                $array_result[$i][$j] = $a;
            }
        }
    }
    else if (!$is_idf && $is_normalization){
        $array_norm = normalization_doc_builder($tf, $tf_method);
        foreach ($tf as $i => $temp1){
            foreach ($temp1 as $j => $v){
                $a = $v / $array_norm[$i];
                $array_result[$i][$j] = $a;
            }
        }
    }
    else if ($is_idf && $is_normalization){
        $array_idf = idf_builder($tf);
        foreach ($tf as $i => $temp1){
            foreach ($temp1 as $j => $v){
                $a = $v * $array_idf[$j];
                $array_temp[$i][$j] = $a;
            }
        }
        $array_norm = normalization_doc_builder($array_temp, $tf_method);
        foreach ($array_temp as $i => $temp1){
            foreach ($temp1 as $j => $v){
                $a = $v / $array_norm[$i];
                $array_result[$i][$j] = $a;
            }
        }
    }
    
    return $array_result;
}

function count_tf_query_input($query_array){
    $index_term = array();
    foreach ($query_array as $temp){
        if (!strrpos($temp, " ")) {
            if (strcasecmp($temp, "") != 0) {
                if (isset($index_term[0][$temp])) {
                    ++$index_term[0][$temp];
                } else {
                    $index_term[0][$temp] = 1;
                }
            }
        }
    }
    return $index_term;
}

function count_tf_query_file($is_stemming, $is_eliminate_stopword, $stopwords, $file_collection){
    $i = "";

    $title_string = ".T";
    $author_string = ".A";
    $body_string = ".W";
    $index_string = ".I";
    $contex_string = ".N";
    
    $stemmer = new Stemmer();

    $is_index = false;
    $is_title = false;
    $is_author = false;
    $is_body = false;
    $is_contex = false;

    $index_term = array();
    $file_col = fopen($file_collection, "r");
    
    if ($file_col){
        while (!feof($file_col)){
            if (($temp_string = fgetcsv($file_col, 10000, " ")) !== FALSE){
                if (strncasecmp($temp_string[0], $index_string, 2) == 0) {
                    //proses pembuatan array of term di dokumen
                    $i = $temp_string[1];
                    $is_index = true;
                } else if (strncasecmp($temp_string[0], $title_string, 2) == 0) {
                    //proses indexing di title
                    $is_title = true;
                } else if (strncasecmp($temp_string[0], $contex_string, 2) == 0) {
                    //proses indexing di publisher
                    $is_publisher = true;
                }else if (strncasecmp($temp_string[0], $author_string, 2) == 0) {
                    //proses indexing di author
                    $is_author = true;
                } else if (strncasecmp($temp_string[0], $body_string, 2) == 0) {
                    //proses indexing di body
                    $is_body = true;
                } else if ((strncasecmp(substr($temp_string[0], 0, 1), ".", 1) == 0) &&
                        ((strncasecmp(substr($temp_string[0], 1, 1), "I", 1) != 0) ||
                        (strncasecmp(substr($temp_string[0], 1, 1), "T", 1) != 0) ||
                        (strncasecmp(substr($temp_string[0], 1, 1), "A", 1) != 0) ||
                        (strncasecmp(substr($temp_string[0], 1, 1), "B", 1) != 0) ||
                        (strncasecmp(substr($temp_string[0], 1, 1), "W", 1) != 0))) {

                    $is_index = false;
                    $is_title = false;
                    $is_author = false;
                    $is_body = false;
                }
                
                if (($is_title || $is_author || $is_body || $is_contex)) {

                    if ((strncasecmp($temp_string[0], $index_string, 2) != 0) &&
                            (strncasecmp($temp_string[0], $title_string, 2) != 0) &&
                            (strncasecmp($temp_string[0], $author_string, 2) != 0) &&
                            (strncasecmp($temp_string[0], $body_string, 2) != 0) &&
                            (strncasecmp($temp_string[0], $contex_string, 2) != 0)) {

                        foreach ($temp_string as $temp) {
                            $temp2 = strtolower($temp);
                            if (!$is_eliminate_stopword || !is_stopword($stopwords, $temp2)) { // disini buang stopwordnya
                                if (!strrpos($temp2, " ")) {
                                    if ($is_stemming) {
                                        $temp2 = $stemmer->stem($temp2); // di sini stemmingnya
                                    }
                                        if (strncasecmp($temp2, "", 1) != 0) {
                                            if (isset($index_term[$i][$temp2])) {
                                                ++$index_term[$i][$temp2];
                                            } else {
                                                $index_term[$i][$temp2] = 1;
                                            }
                                        }
                                    
                                }
                            }
                        }
                    }
                }

            }
        }
    }
    fclose($file_col);
    return $index_term;
}

function tf_query_to_queryarray($tf_query){
    $result = array();
    foreach ($tf_query as $i => $temp_array){
        foreach ($temp_array as $j => $v){
            $result[$i][] = $j;
        }
    }
    return $result;
}

function raw_tf_query($tf){
    return $tf;
}

function binary_tf_query($tf){
    $i = "";
    $j = "";
    $a = $v = 0;
    $log_array = array();
    //$temp_array = count_tf();
    $temp_array = $tf;
    foreach ($temp_array as $i => $temp) {
        foreach ($temp as $j => $v) {
            $log_array[$i][$j] = 1;
        }
    }
    return $log_array;
    //print_r($log_array);
}

function log_tf_query($tf){
    $i = "";
    $j = "";
    $a = $v = 0;
    $log_array = array();
    //$temp_array = count_tf();
    $temp_array = $tf;
    foreach ($temp_array as $i => $temp) {
        foreach ($temp as $j => $v) {
            if ($v != 0) {
                $a = 1 + log10($v);
                $log_array[$i][$j] = $a;
            } else if ($v == 0) {
                $log_array[$i][$j] = 0;
            }
        }
    }
    return $log_array;
    //print_r($log_array);
}

function augmented_tf_query($tf){
    $i = "";
    $j = "";
    $a = $v = 0;
    $temp_max = 0;
    $log_array = array();
    $temp_array = $tf;
    foreach ($temp_array as $i => $temp) {
        $temp_max = max($temp);
        foreach ($temp as $j => $v) {
            $a = 0.5 + 0.5 * $v / $temp_max;
            $log_array[$i][$j] = $a;
        }
    }
    return $log_array;
    //print_r($log_array);
}

function normalization_query_builder($tf_query, $tf_method){
    // raw, logarithmic, binary, augmented
    $i = $j = "";
    $nor_array = array();
    $temp_array = array();
    $temp_value = 0;
    $a = 0;
    if (strcasecmp($tf_method, "raw") == 0) {
        $temp_array = raw_tf_query($tf_query);
    } else if (strcasecmp($tf_method, "logarithmic") == 0) {
        $temp_array = log_tf_query($tf_query);
    } else if (strcasecmp($tf_method, "binary") == 0) {
        $temp_array = binary_tf_query($tf_query);
    } else if (strcasecmp($tf_method, "augmented") == 0) {
        $temp_array = augmented_tf_query($tf_query);
    }
    
    foreach ($temp_array as $i => $temp_1){
        $temp_value = 0;
        foreach ($temp_1 as $j => $v){
            $a = $v * $v;
            $temp_value += $a;
        }
        $nor_array[$i] = sqrt($temp_value);
    }
    
    return $nor_array;
}

function term_weighting_query($array_idf, $tf_query, $is_idf, $is_normalization, $tf_method){
    $array_result = array();
    $i = $j = "";
    $a = $b = 0;
    $array_norm = array();
    $array_temp = array();
    
    if (!$is_idf && !$is_normalization){
        $array_result = $tf_query;
    }
    else if ($is_idf && !$is_normalization){
        foreach ($tf_query as $i => $temp1){
            foreach ($temp1 as $j => $v){
                if (isset ($array_idf[$j])){
                    $a = $v * $array_idf[$j];
                    $array_result[$i][$j] = $a;
                }
                else{
                    $array_result[$i][$j] = 0;
                }
            }
        }
    }
    else if (!$is_idf && $is_normalization){
        $array_norm = normalization_query_builder($tf_query, $tf_method);
        foreach ($tf_query as $i => $temp1){
            foreach ($temp1 as $j => $v){
                $a = $v / $array_norm[$i];
                $array_result[$i][$j] = $a;
            }
        }
    }
    else if ($is_idf && $is_normalization){
        foreach ($tf_query as $i => $temp1){
            foreach ($temp1 as $j => $v){
                if (isset ($array_idf[$j])){
                    $a = $v * $array_idf[$j];
                    $array_temp[$i][$j] = $a;
                }
                else{
                    $array_temp[$i][$j] = 0;
                }
            }
        }
        
        $array_norm = normalization_query_builder($array_temp, $tf_method);
        foreach ($array_temp as $i => $temp1){
            foreach ($temp1 as $j => $v){
                $a = $v / $array_norm[$i];
                $array_result[$i][$j] = $a;
            }
        }
    }
    
    return $array_result;
}

function sorting_similarity($array_similarity){
    $array_result = array();
    $array_temp = $array_similarity;
    foreach ($array_temp as $i => $temp){
        arsort($temp);
        $array_result[$i] = $temp;
    }
    return $array_result;
    
}

function similarity($array_weight_doc, $array_weight_query){
    $array_of_similarity  = array();
    $array_of_similarity_sorted = array();
    $temp_value = 0;
    $i_query = "";
    $i_doc = $j_doc = "";
    
    foreach ($array_weight_query as $i_query => $w_query){
        foreach ($array_weight_doc as $i_doc => $w_doc){
            $temp_value = 0;
            foreach ($w_doc as $j_doc => $v_doc){
                $temp_value += ($w_query[$j_doc] * $v_doc);
            }
            $array_of_similarity[$i_query][$i_doc] = $temp_value;
        }
    }
    
    $array_of_similarity_sorted = sorting_similarity($array_of_similarity);
    return $array_of_similarity_sorted;
}


function similarity_eks($array_weight_doc, $array_weight_query, $array_of_similarity, $i_query){
    //$array_of_similarity  = array();
    //$array_of_similarity_sorted = array();
    $array_temp = array();
    $temp_value = 0;
    //$i_query = "";
    $i_doc = $j_doc = "";
    
    foreach ($array_weight_doc as $i_doc => $w_doc){
        $temp_value = 0;
        foreach ($w_doc as $j_doc => $v_doc){
            $temp_value += $array_weight_query[0][$j_doc] * $v_doc;
        }
        $array_of_similarity[$i_query][$i_doc] = $temp_value;
    }
    
    //$array_of_similarity_sorted = sorting_similarity($array_of_similarity);
    return $array_of_similarity;
}

?>
